package com.wallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wallet.Exception.WalletException;
import com.wallet.dao.OrderMapper;
import com.wallet.dao.RecordMapper;
import com.wallet.dao.UserMapper;
import com.wallet.model.dto.UserDto;
import com.wallet.model.po.Order;
import com.wallet.model.po.Record;
import com.wallet.model.po.User;
import com.wallet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @description 用户服务实现类
 * @date 2023/2/27 0027 16:14
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RecordMapper recordMapper;

    /**
     * 查询用户余额
     * @param id 用户id
     * @return User对象
     */
    @Override
    public User getUser(Long id) {
        return userMapper.selectById(id);
    }

    /**
     * 用户消费实现
     * @param userDto 用户基本信息以及此次消费金额
     * @return 用户对象
     * @throws Exception
     */
    @Override
    @Transactional
    public User consume(UserDto userDto) throws Exception {
        Long userDtoId = userDto.getId();
        User user = userMapper.selectById(userDtoId);
        if(user==null){
            throw new Exception("用户不存在！");
        }
        Double balance = user.getBalance();//用户余额
        Double price = userDto.getPrice();//用户消费金额
        BigDecimal bigBalance=new BigDecimal(balance);
        BigDecimal bigPrice=new BigDecimal(price);
        BigDecimal subtract = bigBalance.subtract(bigPrice);
        balance = subtract.doubleValue();
        if(balance<0){
            WalletException.cast("余额不足！");
        }
        user.setBalance(balance);
        userDto.setBalance(balance);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userDtoId);
        //更新用户余额
        userMapper.update(userDto,queryWrapper);
        //向订单表添加记录
        Order order=new Order();
        order.setUserId(user.getId());
        order.setPrice(price);
        order.setCreateTime(LocalDateTime.now());
        orderMapper.insert(order);

        //向记录表添加记录
        Record record=new Record();
        record.setUserId(user.getId());
        record.setOperate(-price); //负号表示消费操作
        record.setCreateTime(LocalDateTime.now());
        recordMapper.insert(record);

        return user;
    }

    /**
     * 用户退款
     * @param id 订单id
     * @return 用户对象
     * @throws Exception
     */
    @Override
    @Transactional
    public User chargeback(Long id) throws Exception {
        Order chargebackOrder = orderMapper.selectById(id);
        if(chargebackOrder==null){
            throw new Exception("订单不存在");
        }
        //删除订单记录
        orderMapper.deleteById(id);
        Double price = chargebackOrder.getPrice();
        Long userId = chargebackOrder.getUserId();
        User user = userMapper.selectById(userId);
        Double balance = user.getBalance();
        BigDecimal bigPrice=new BigDecimal(price);
        BigDecimal bigBalance=new BigDecimal(balance);
        BigDecimal add = bigBalance.add(bigPrice);
        double v = add.doubleValue();
        user.setBalance(v);
        //更新用户余额
        userMapper.updateById(user);

        //向操作记录表插入记录
        Record record=new Record();
        record.setUserId(user.getId());
        record.setOperate(+price);
        record.setCreateTime(LocalDateTime.now());
        recordMapper.insert(record);

        return userMapper.selectById(userId);
    }

    /**
     * 用户查询钱包明细
     * @param id 用户id
     * @return 操作记录的集合
     */
    @Override
    public List<Record> getList(Long id) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getUserId,id);
        return recordMapper.selectList(queryWrapper);
    }
}
